#coding=utf-8

import urllib
import urllib2
import cookielib
import json
import random
import sys
import os
import time



def getRestList(offset,longitude,latitude):
	print '爬取 '+str(longitude)+','+str(latitude)+' 餐厅,目标数据 ' + str(offset) + ' - ' + str(offset+30)
	url 		=	'https://www.ele.me/restapi/shopping/restaurants?extras%5B%5D=activities&geohash=wtw3evq7uss&latitude='+str(latitude)+'&limit=30&longitude='+str(longitude)+'&sign='+str(1506307800000+random.randint(0,9999))+'&terminal=web&offset=' + str(offset)
	print url
	userAgent	= 	'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36'
	headers		=	{
						'User-Agent':userAgent,
						'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
					}
	data		=	urllib.urlencode({})
	request 	=	urllib2.Request(url+'?'+data,headers=headers)
	restList 	=	[]
	try:
		response 	= 	urllib2.urlopen(request)
		body = response.read().decode('utf-8');
		body = json.loads(body)
		for restaurant in body:
			restList += [restaurant['name']]
	except urllib2.URLError, e:
		print 'error - %d' % (e.code)

	return restList


# 去重
def repeatScreen(allList):
	tempList=[]
	for name in allList:
		name+="（("
		name=name[0:min(name.index("（"),name.index("("))]
		tempList+=[name]#把店名()删掉
	return [i for i in set(tempList)]

# 黑名单
def removeBlock(allList,blacklist):
	tempList=[]
	for name in allList:
		isBlack = False
		for blackkey in blacklist:
			if name.find(blackkey) != -1:
				isBlack=True
				break
		if isBlack:
			continue
		tempList+=[name]
	return tempList

def __main__():
	# 编译环境
	reload(sys)
	sys.setdefaultencoding('utf8')
	print sys.version   
	#print sys.version_info  

	# 当天数据key
	localtime = time.localtime(time.time())
	today = "eleCache."+str(localtime.tm_year)+"."+str(localtime.tm_mon) +"."+ str(localtime.tm_mday)
	# yesterTime = time.gmtime(time.time()-60*60*24)
	# yesterday = "eleCache."+str(yesterTime.tm_year)+"."+str(yesterTime.tm_mon) +"."+ str(yesterTime.tm_mday)
	print today

	# 读取文件中的数据
	cacheName = "ele.cache"##{"cachelist":["name1","name2"],"cachetime":"eleCache.2017.11.7"}
	cacheTimekey = "cachetime"
	cacheDataKey = "cachelist"
	allList = []
	if os.path.isfile(cacheName):
		file = open(cacheName,"rb")#只读
		cacheStr = file.read()
		print cacheStr
		if len(cacheStr)==0:
			cacheStr="{\"cachelist\":[],\"cachetime\":\"\"}"
		decoded = json.loads(cacheStr)
		print decoded[cacheTimekey]
		if decoded[cacheTimekey] != today :
			print "no today"#Syrio:there is only one God and his name is Death. and there is only one thing we say to Death: Not today.
		elif type(decoded[cacheDataKey])!=list:
			print "缓存不是数组"
		else:
			#缓存数据
			allList=decoded[cacheDataKey]
		file.close()

	if(len(allList) ==0):#没有缓存数据
		#每次最多查询30条数据
		count   = 30*5
		allList = []
		while len(allList) < count:
			allList += getRestList(len(allList),121.46264,31.23068)
		#保存数据
		file = open(cacheName,"w+")
		file.write(json.dumps({cacheDataKey:allList,cacheTimekey:today}))
		file.close()	


	len1 = len(allList)
	print '总数\t'+str(len1)

	allList = repeatScreen(allList)
	len1 = len(allList)
	print '去重后\t'+str(len1)

	blacklist = ["粥","沙拉","寿司","油条","包子","馒头","点心","葳姐","饼","早餐","汤圆"]
	allList = removeBlock(allList,blacklist)
	len1 = len(allList)
	print '黑名单屏蔽后\t'+str(len1)
	# for name in allList:
	# 	print name.encode("utf-8")


	for index in range(0,6):#六个选项
		len1 = len(allList)
		randomIndex = random.randint(0,len1-1)
		name = (allList[randomIndex]).encode('utf-8')
		del allList[randomIndex]
		print '随机种子:' + str(randomIndex) + '\t\tres : ' + name


__main__()
